﻿CREATE PROCEDURE [usix].[s_Payments_Update]--1,1
	@IsSync bit = 0,
	@ReLoad bit = 0
AS

IF @IsSync = 0 BEGIN
	EXEC sp__sched_Job 'Run s_Payments_Update','EXEC usix.s_Payments_Update 1'
	RETURN
END

BEGIN TRAN --[1

IF @ReLoad = 0 BEGIN
DECLARE @Today datetime SET @Today = dbo.Today(GETDATE())-4
DELETE usix.t_Payment WHERE [System Date] >= @Today

INSERT INTO usix.t_Payment
SELECT     [Id #], [Receipt #], [Payment Date], [Payment Amount], [Late Charge], [Payment Due Date], [Payment Type], [Payment Form], [How Paid], 
                      [# of Payments], [System Date], [Int Prt_], [L_C Prt_], [NSF Prt_], [DLR Prt_], [Fld# 013], [Fld# 170], [L_C Date _Fld# 183)], [Fld# 186], [Fld# 187], 
                      [Int Accrue], [Past Due], Balance, L_UL, CompanyID
FROM         DataMining.dbo.ALL_Payments WITH(nolock)
WHERE [System Date] >= @Today

IF @@ERROR <> 0 BEGIN ROLLBACK RETURN END

END
ELSE BEGIN

	TRUNCATE TABLE usix.t_Payment
	INSERT INTO usix.t_Payment
	SELECT *  FROM DataMining.dbo.ALL_Payments WITH(nolock)
	IF @@ERROR <> 0 BEGIN ROLLBACK RETURN END

END

COMMIT --1]

EXEC s_PromisesToPay_Update
TRUNCATE TABLE usix.t_PaymentNSF
INSERT INTO usix.t_PaymentNSF
SELECT * FROM usix.v_PaymentNSF
